package westh.ilib.service.surveyReport.documentSearch;

import java.io.File;
import java.util.Date;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;

import westh.ilib.service.surveyReport.queryGeneration.Generator;

/**
 * search docs in local index (both local dataset & croDomainSearch result)
 * @param indexPath: the directory path of index
 * @param wordnetIndexPath: the directory path of wordnetIndex (for query expansion)
 * @param String str: query keywords / @param Query query: query generated from str
 * @param n: get top n docments as result
 * @param scores: hit documents' score by Lucene
 * @param years: hit documents' published year
 * @param confs: hit documents' publisher
 * @param titles: hit documents' title
 * @param keywords: hit documents' keyword
 * @param summarys: hit documents' summary
 * @param rscType: hit docuements' rscType
 * @author Zhang Yanxing
 * 2010-8-19
 */
public class docSearcher {

	// search the index, get top n results,
	// store in scores, years, confs, titles, keywords, summarys
	public static void search(String indexPath, String wordnetIndexPath, String str, int n, float[] scores,
			String[] years, String[] confs, String[] titles, String[] keywords,
			String[] summarys, int[] rscType) {
		try {
			// read in the index
			IndexReader reader = IndexReader.open(FSDirectory.open(new File(
					indexPath)), true); // only searching, so read-only=true
			Searcher searcher = new IndexSearcher(reader);

			// generate the query
			Generator gen = new Generator(str);
			Query query = gen.generator(wordnetIndexPath);
			System.out.println("Searching for: " + query.toString());

			// search top n results
			Date start = new Date();
			TopDocs result = searcher.search(query, null, n);
			Date end = new Date();
			System.out.println("Time: " + (end.getTime() - start.getTime())
					+ "ms");

			ScoreDoc[] scoreDocs = result.scoreDocs;
			System.out.println(result.totalHits + "\t" + scoreDocs.length);

			// get the fields & store in the return arrays
			for (int i = 0; i < scoreDocs.length; i++) {
				scores[i] = scoreDocs[i].score;
				years[i] = searcher.doc(scoreDocs[i].doc).get("year");
				confs[i] = searcher.doc(scoreDocs[i].doc).get("conf");
				titles[i] = searcher.doc(scoreDocs[i].doc).get("title");
				keywords[i] = searcher.doc(scoreDocs[i].doc).get("keyword");
				summarys[i] = searcher.doc(scoreDocs[i].doc).get("summary");
				rscType[i] = Integer.valueOf(searcher.doc(scoreDocs[i].doc).get("rscType"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// search the index, use Query as input
	public static void search(String indexPath, Query query, int n, float[] scores,
			String[] years, String[] confs, String[] titles, String[] keywords,
			String[] summarys, int[] rscType) {
		
		try {
			IndexReader reader = IndexReader.open(FSDirectory.open(new File(
					indexPath)), true); // only searching, so read-only=true
			Searcher searcher = new IndexSearcher(reader);

			System.out.println("Searching for: " + query.toString());

			Date start = new Date();
			TopDocs result = searcher.search(query, null, n);
			Date end = new Date();
			System.out.println("Time: " + (end.getTime() - start.getTime())
					+ "ms");

			ScoreDoc[] scoreDocs = result.scoreDocs;
			//total hits is less than n
			if(scoreDocs.length < n){
				n = scoreDocs.length;
			}

			for (int i = 0; i < scoreDocs.length; i++) {
				scores[i] = scoreDocs[i].score;
				years[i] = searcher.doc(scoreDocs[i].doc).get("year");
				confs[i] = searcher.doc(scoreDocs[i].doc).get("conf");
				titles[i] = searcher.doc(scoreDocs[i].doc).get("title");
				keywords[i] = searcher.doc(scoreDocs[i].doc).get("keyword");
				summarys[i] = searcher.doc(scoreDocs[i].doc).get("summary");
				if(searcher.doc(scoreDocs[i].doc).get("rscType") != null){
					rscType[i] = Integer.valueOf(searcher.doc(scoreDocs[i].doc).get("rscType"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		/* unit test */
		final String indexPath = "E:\\zhyx\\SurveyReport\\index\\";

		// directory of index of wordNet synonyms
		final String wordnetIndexPath = "E:\\zhyx\\SurveyReport\\wordnetIndex";

		int n = 50;
		float[] scores = new float[n];
		String[] years = new String[n];
		String[] confs = new String[n];
		String[] titles = new String[n];
		String[] keywords = new String[n];
		String[] summarys = new String[n];
		int[] rscType = new int[n];
		    
		String str = "search engine";
		Generator gen = new Generator(str);
		Query query = gen.generator(wordnetIndexPath);
		docSearcher.search(indexPath, query, n, scores, years, confs, titles, keywords,
				summarys, rscType);
		for (int i = 0; i < n; i++) {
			System.out.println(i + "\t" + scores[i] + "\t" + years[i] + "\t"
					+ confs[i] + "\n\n" + titles[i] + "\n\n" + keywords[i]
					+ "\n\n" + summarys[i] + "\n\n");
		}
	}
}